package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜肴管理
 */
@RestController(value="adminDishController")
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags="菜肴相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String dish_="DISH_";
    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping("")
    @ApiOperation("新增菜品")
    public Result<String> addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.addDish(dishDTO);
        //删除缓存
        redisTemplate.delete(dish_+dishDTO.getCategoryId());
        return Result.success();
    }


    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> pageQueryDish(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询，参数:{}",dishPageQueryDTO);
        PageResult pageResult=dishService.pageQueryDish(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品，支持批量删除
     * ①起售菜品不能删除
     * ②被套餐关联菜品不能删除
     * ③菜品对应口味需一并删除
     * @return
     */
    @DeleteMapping("")
    @ApiOperation("删除菜品")
    public Result<String> deleteDish(@RequestParam List<Long> ids){
        log.info("菜品批量删除:{}",ids);
        dishService.deleteDishByIds(ids);
        //删除所有菜品缓存
        deleteAllDishCache();
        return Result.success();
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("获取菜品信息")
    public Result<DishVO> getDishById(@PathVariable Long id){
        log.info("根据id查询菜品");
        DishVO dishVO=dishService.getDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     * @return
     */
    @PutMapping("")
    @ApiOperation("修改菜品")
    public Result<String> updateDishInfo(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{},",dishDTO);
        dishService.updateDishInfo(dishDTO);
        //修改菜品可能涉及修改菜品的CategoryID操作，不能仅删除传入的CategoryID
        deleteAllDishCache();
        return Result.success();
    }

    /**
     * 修改菜品售卖状态
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品售卖状态")
    public Result<String> switchStatus(@PathVariable Integer status, Long id){
        log.info("修改菜品售卖状态");
        dishService.switchStatus(status,id);
        deleteAllDishCache();
        return Result.success();
    }

    /**
     * 获取在售菜品列表
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("获取在售菜品；列表")
    public Result<List<Dish>> listDishByCategoryId(Long categoryId){
        log.info("根据类目id获取菜品列表：{}",categoryId);
        List<Dish> list=dishService.listDish(categoryId);
        return Result.success(list);
    }

    /**
     * 清理菜品缓存
     */
    private void deleteAllDishCache(){
        Set keySet=redisTemplate.keys(dish_+"*");
        redisTemplate.delete(keySet);
    }
}
